package state

import (
	"net"
	"sync"
	"time"

	"github.com/gorilla/websocket"
)

type SlaveConn struct {
	Once        sync.Once
	Vaild       bool
	Dead        bool
	Conn        *websocket.Conn
	Laddr       string       // 本地被映射的端口
	Raddr       string       // 远端地址
	Resaddr     string       // 客户端需要连接进来的端口
	Listener    net.Listener // 被映射端口监听
	Reslistener net.Listener // 客户端连接监听
	Wschan      chan []byte
	Stoping     chan struct{}
	Notify      chan struct{}
	Begin       time.Time
}

func (slf *StatusStruct) FindSlaveWSConn(raddr string) *SlaveConn {
	slf.mu.Lock()
	defer slf.mu.Unlock()

	return slf.slavewscon[raddr]
}

func (slf *StatusStruct) UpdateSlaveWSConn(raddr string, sc *SlaveConn) {
	slf.mu.Lock()
	defer slf.mu.Unlock()

	slf.slavewscon[raddr] = sc
}

func (slf *StatusStruct) RemoveSlaveWSConn(raddr string) {
	slf.mu.Lock()
	defer slf.mu.Unlock()

	delete(slf.slavewscon, raddr)
}

func (slf *StatusStruct) SlaveConns() map[string]*SlaveConn {
	ret := map[string]*SlaveConn{}
	slf.mu.Lock()
	defer slf.mu.Unlock()

	for k, v := range slf.slavewscon {
		ret[k] = v
	}

	return ret
}
